package kr.saeildamil.androidStudy_second.fragment.common;

import kr.saeildamil.androidStudy_second.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class CounterFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		/* 방법1
		View root = inflater.inflate(R.layout.ch20_counterfragment, container);
		final TextView textcounter = (TextView) root.findViewById(R.id.ch20_counterfrag_text);
		root.findViewById(R.id.ch20_counterfrag_btn).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int count = Integer.parseInt(textcounter.getText().toString());
				textcounter.setText(Integer.toString(count+1));
				
			}
		});*/
		
		//방법2 : 직접 생성하기
		/*LinearLayout root = new LinearLayout(getActivity());
		root.setOrientation(LinearLayout.VERTICAL);
		root.setBackgroundColor(Color.YELLOW);
		root.setGravity(Gravity.CENTER_HORIZONTAL);
		
		
		final TextView textcounter = new TextView(getActivity());
		textcounter.setText("0");
		textcounter.setTextColor(Color.RED);
		textcounter.setTextSize(40);
		
		Button btnincrease = new Button(getActivity());
		btnincrease.setText("Increase");
		
		LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		root.addView(textcounter, param);
		root.addView(btnincrease, param);
		
		btnincrease.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int count = Integer.parseInt(textcounter.getText().toString());
				textcounter.setText(Integer.toString(count + 1));
			}
		});*/
		
		//방법 3: 액티비티의 차일드 검색하기 -다운됨
		/*View root = inflater.inflate(R.layout.ch20_counterfragment, container);
		Button btnIncrease = (Button) getActivity().findViewById(R.id.ch20_counterfrag_btn);
		final TextView textcounter = (TextView) getActivity().findViewById(R.id.ch20_counterfrag_text);
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int count = Integer.parseInt(textcounter.getText().toString());
				textcounter.setText(Integer.toString(count + 1));
				
			}
		});*/
		//방법 4: onActivityCreated에서 getActivity로 검색하여 핸들러 설치
	/*	View root = inflater.inflate(R.layout.ch20_counterfragment, container);
		
		return root;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Button btnIncrease = (Button) getActivity().findViewById(R.id.ch20_counterfrag_btn);
		final TextView textcounter = (TextView) getActivity().findViewById(R.id.ch20_counterfrag_text);
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int count = Integer.parseInt(textcounter.getText().toString());
				textcounter.setText(Integer.toString(count + 1));
				
			}
		});*/
		
		//방법 5: onActivityCreated에서 getView로 검색하여 핸들러 설치
		View root = inflater.inflate(R.layout.ch20_counterfragment, container);
		
		return root;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Button btnIncrease = (Button) getView().findViewById(R.id.ch20_counterfrag_btn);
		final TextView textcounter = (TextView) getView().findViewById(R.id.ch20_counterfrag_text);
		
		btnIncrease.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int count = Integer.parseInt(textcounter.getText().toString());
				textcounter.setText(Integer.toString(count + 1));
				
			}
		});
		
	}

}
